package com.maxjay.system.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;

import com.maxjay.common.util.date.DateUtil;
import com.maxjay.system.service.SchedulerService;

/**
 * 有状态job和无状态job调度器基类
 * 
 * @author Administrator
 *
 */
public class SchedulerServiceImpl implements SchedulerService{
	
	private static Logger logger = Logger.getLogger(SchedulerServiceImpl.class);
	
	@Resource(name = "quartzScheduler")
	protected Scheduler scheduler;

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}
	
	/**
	 * 根据表达式形式的触发器构造一个定时任务
	 * 
	 * @param jobDetail
	 * @param name jobDetail名字
	 * @param startTime String 任务开始时间，日期(时间)字符串 20090801
	 * @param endTime String 任务结束时间，日期(时间)字符串 20090801
	 * @param cronExpression
	 */
	public void schedule(JobDetail jobDetail, String name, String startTime, String endTime, String cronExpression) {
		try {
			Date startTimeDate = null;
			Date endTimeDate = null;
			if(StringUtils.isNotBlank(startTime)){
				startTimeDate = DateUtil.changeStrToDate(startTime);
			}
			if(StringUtils.isNotBlank(endTime)){
				endTimeDate = DateUtil.changeStrToDate(endTime);
			}
			schedule(jobDetail, name, startTimeDate, endTimeDate, new CronExpression(cronExpression));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 根据表达式形式的触发器构造一个定时任务
	 * 
	 * @param jobDetail
	 * @param name jobDetail名字
	 * @param startTime Date 任务开始时间
	 * @param endTime Date 任务结束时间
	 * @param cronExpression
	 */
	protected void schedule(JobDetail jobDetail, String name, Date startTime, Date endTime, CronExpression cronExpression) {
		if (StringUtils.isBlank(name)) {
			name = UUID.randomUUID().toString();
		}
		try {
			scheduler.addJob(jobDetail, true);
			if(startTime == null){
				startTime = new Date();
			}
			CronTrigger cronTrigger = new CronTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),
					Scheduler.DEFAULT_GROUP);
			cronTrigger.setCronExpression(cronExpression);
			if(endTime != null){
				cronTrigger.setEndTime(endTime);
			}
			scheduler.scheduleJob(cronTrigger);
			scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, cronTrigger);
			logger.info("启动了一个名字为:" + jobDetail.getFullName() + "的调度任务");
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}
	

	/**
	 * 根据一个固定时间来构造一个定时任务
	 * 
	 * @param jobDetail
	 * @param name
	 * @param String 任务开始时间，日期(时间)字符串 20090801
	 * @param endTime String 任务结束时间，日期(时间)字符串 20090801
	 * @param repeatCount 任务重复次数
	 * @param repeatInterval 间隔时间
	 */
	public void schedule(JobDetail jobDetail, String name, String startTime, String endTime, int repeatCount,
			long repeatInterval) {
		Date startTimeDate = DateUtil.changeStrToDate(startTime);
		Date endTimeDate = DateUtil.changeStrToDate(endTime);
		this.schedule(jobDetail, name, startTimeDate, endTimeDate, repeatCount,	repeatInterval);
	}

	/**
	 * 根据一个固定时间来构造一个定时任务
	 * 
	 * @param jobDetail
	 * @param name
	 * @param startTime Date 任务开始时间
	 * @param endTime Date 任务结束时间
	 * @param repeatCount 任务重复次数
	 * @param repeatInterval 间隔时间
	 */
	protected void schedule(JobDetail jobDetail, String name, Date startTime, Date endTime, int repeatCount, 
			long repeatInterval) {
		if (StringUtils.isBlank(name)) {
			name = UUID.randomUUID().toString();
		}
		try {
			scheduler.addJob(jobDetail, true);
			if(startTime==null){
				startTime = new Date();
			}
			SimpleTrigger SimpleTrigger = new SimpleTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),
					Scheduler.DEFAULT_GROUP, startTime, endTime, repeatCount, repeatInterval);
			scheduler.scheduleJob(SimpleTrigger);
			scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, SimpleTrigger);
			logger.info("启动了一个名字为:" + jobDetail.getFullName() + "的调度任务");
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void destroy(){
		try {
			this.scheduler.shutdown();
			logger.info("销毁scheduler");
		} catch (SchedulerException e) {
			e.printStackTrace();
			logger.error(e.getCause(),e);
		}
	}

	/**
	 * 移除指定任务
	 * 
	 * @param jobName
	 * @param jobGroupName
	 */
	public void deleteJob(String jobName, String jobGroupName) {
		try {
			this.scheduler.deleteJob(jobName, jobGroupName);
			logger.info("销毁一个 名字为 name:" + jobName +" jobGroup:" +jobGroupName + "的调度任务");
		} catch (SchedulerException e) {
			e.printStackTrace();
			logger.error(e.getCause(),e);
		}
	}
	
	
}
